#coding=utf-8
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django import forms

from controller.controller import Controller
from util.fileuploadhandler import handle_uploaded_file

controller = Controller();

def home(request):
    return render_to_response('home.html')

def start_test(request):
    #controller.set_dest("http://intake21vm1.intake.vc.noklab.net/default/index")
    controller.test()
    return HttpResponse()

def set_url(request):
    url = request.read()
    controller.config.set_url(url)
    return HttpResponse()

class UploadFileForm(forms.Form):
    #title = forms.CharField(max_length=50)
    file = forms.FileField()
    
def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            filename = handle_uploaded_file(request.FILES['file'])
            controller.get_cases(filename)
            return HttpResponseRedirect('/osimtest/home')
    else:
        form = UploadFileForm()
        
    return HttpResponse()